package com.hotel.project.demo.controller;

import com.hotel.project.common.api.CommonResult;
import com.hotel.project.demo.api.HotelResultCode;
import com.hotel.project.demo.domain.HotelAnswerEntity;
import com.hotel.project.demo.domain.HotelQuestionEntity;
import com.hotel.project.demo.domain.ResponseBean;
import com.hotel.project.demo.service.HotelAnswerService;
import com.hotel.project.demo.service.impl.HotelAnswerServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/hotel/answer")
public class HotelAnswerController extends BaseController<HotelAnswerService, HotelAnswerEntity>{

    @GetMapping("/list-by-question-id")
    public CommonResult<List<HotelAnswerEntity>> listHotelAnswerEntityByQuestionId(@RequestParam("id") Long id) {
        List<HotelAnswerEntity> list = ((HotelAnswerServiceImpl)service).listHotelAnswerEntityByQuestionId(id);
        if (list != null && list.size() > 0){
            return CommonResult.success(list,"获取回答成功");
        }
        return CommonResult.failed(HotelResultCode.FAILED,"获取问题失败");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除回答", notes = "删除回答")
    public ResponseBean deleteAnswerById(@RequestParam("id") Long id) {
        ResponseBean rb = new ResponseBean();
        boolean flag = service.removeById(id);
        rb.setCode(10000L);
        rb.setMessage("删除成功");
        if (flag) {
            return rb;
        }
        return errorEdit(rb, 400L);
    }

    @DeleteMapping("/save")
    @ApiOperation(value = "新建回答", notes = "新建回答")
    public ResponseBean saveQuestion(HotelAnswerEntity entity) throws Exception{
        ResponseBean rb = new ResponseBean();
        try{
            service.saveOrUpdate(entity);
            rb.setData(entity);
            rb.setCode(10000L);
            rb.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            rb.setMessage("添加失败");
        }
        return rb;
    }


}
